package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-05-05 10:45
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 将商品加入到购物车
     *
     * @param skuId  商品SKUID
     * @param skuNum 商品数量
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
        //获取用户ID(真实用户ID或者临时用户ID)
        //1.优先使用登陆后用户ID
        String userId = AuthContextHolder.getUserId(request);

        //2.用户未登录再获取临时用户ID
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }

        //3.调用业务层将商品加入到购物车
        cartService.addToCart(userId, skuId, skuNum);
        return Result.ok();
    }


    /**
     * 查询当前用户(登录用户,临时用户)购物车商品列表
     *
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result getCartList(HttpServletRequest request) {
        //获取用户ID(真实用户ID或者临时用户ID)
        //1.优先使用登陆后用户ID
        String userId = AuthContextHolder.getUserId(request);

        //2.用户未登录再获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);

        //3.调用业务层获取购物车列表数据
        List<CartInfo> cartInfoList = cartService.getCartList(userId, userTempId);
        return Result.ok(cartInfoList);
    }


    /**
     * 修改购物车中商品选中状态
     *
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("isChecked") Integer isChecked) {
        //获取用户ID(真实用户ID或者临时用户ID)
        //1.优先使用登陆后用户ID
        String userId = AuthContextHolder.getUserId(request);

        //2.用户未登录再获取临时用户ID
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.checkCart(userId, skuId, isChecked);
        return Result.ok();
    }

    /**
     * 删除指定购物车商品
     * @param request
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request, @PathVariable("skuId") Long skuId){
        //获取用户ID(真实用户ID或者临时用户ID)
        //1.优先使用登陆后用户ID
        String userId = AuthContextHolder.getUserId(request);

        //2.用户未登录再获取临时用户ID
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(userId, skuId);
        return Result.ok();
    }
}
